<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use Exception;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;
/**
 * 企业名录
 *
 * @icon fa fa-circle-o
 */
class LitestoreEUser extends Backend
{
    
    /**
     * LitestoreEUser模型对象
     * @var \app\admin\model\LitestoreEUser
     */
    protected $model = null;
    protected $searchFields = "id,nickname,companyname";

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('User');
        $this->view->assign("certifyStatusList", $this->model->getCertifyStatusList());
        $this->view->assign("apprStatusList", $this->model->getApprStatusList());
        $glarr = array('2'=>'原料供应商','3'=>'机制炭生产企业','4'=>'机制炭批发商','5'=>'设备供应商','6'=>'果木炭生产企业','7'=>'生产师父','8'=>'其它');



        $this->view->assign('groupList', build_select('row[group_id]', $glarr, [], ['class' => 'form-control selectpicker']));
        $this->view->assign("levelList", $this->model->getLevelList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
       /*     $where['group_id'] =array('IN',array('7')); */
            $total = $this->model
                        //where('id','IN',$ids)
                    ->where('group_id','IN',['2','3','4','5','6','8'])
                    ->where('status','normal')                     
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
            ->where('group_id','IN',['2','3','4','5','6','8'])
            ->where('status','normal')                     
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            foreach ($list as $row) {
                $row->visible(['id','group_id','level','nickname','mobile','score','avatar','region_city','address','work','experience','companyname','businessscope','businesslicense_images','companyprofile','certify_status','appr_status','appr_fail_reson']);
                
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
    /**
     * 增加
     */
    public function add($ids = NULL)
    {

        //$this->view->assign('groupList', build_select('row[group_id]', \app\admin\model\UserGroup::column('id,name'),[] , ['class' => 'form-control selectpicker']));
        $this->view->assign('cardtplList', build_select('row[card_tpl_id]', \app\admin\model\LitestoreCardTpl::column('id,name'), [], ['class' => 'form-control selectpicker']));
        return parent::add();
    }

    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        $row = $this->model->get($ids);
        if (!$row)
            $this->error(__('No Results were found'));
            $glarr = array('2'=>'原料供应商','3'=>'机制炭生产企业','4'=>'机制炭批发商','5'=>'设备供应商','6'=>'果木炭生产企业','8'=>'其它');
        $this->view->assign('groupList', build_select('row[group_id]',$glarr, $row['group_id'], ['class' => 'form-control selectpicker']));
        $this->view->assign('cardtplList', build_select('row[card_tpl_id]', \app\admin\model\LitestoreCardTpl::column('id,name'), $row['card_tpl_id'], ['class' => 'form-control selectpicker']));
        return parent::edit($ids);
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids) {
            $pk = $this->model->getPk();
            $adminIds = $this->getDataLimitAdminIds();
            if (is_array($adminIds)) {
                $this->model->where($this->dataLimitField, 'in', $adminIds);
            }
            $list = $this->model->where($pk, 'in', $ids)->select();

            $count = 0;
            Db::startTrans();
            try {
                foreach ($list as $k => $v) {
                    //$count += $v->delete();
                    $count += $count+1;
                    DB::execute("update fa_user set status='hidden' where id = ".$v['id']."",[]);
                    DB::execute('delete from fa_third where user_id = '.$v['id'].'',[]);
                    //Db::name('third')->where(['user_id'=> $v['id']])->delete();
                }
                Db::commit();
            } catch (PDOException $e) {
                Db::rollback();
                $this->error($e->getMessage());
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if ($count) {
                $this->success();
            } else {
                $this->error(__('No rows were deleted'));
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }


    /**
     * 编辑
     */
    public function appr22($ids = NULL)
    {
        $row = $this->model->get($ids);
        if (!$row)
            $this->error(__('No Results were found'));
        //$this->view->assign('groupList', build_select('row[group_id]', \app\admin\model\UserGroup::column('id,name'), $row['group_id'], ['class' => 'form-control selectpicker']));
        $this->view->assign('row',$row);
        return $this->view->fetch();
     //   return parent::edit($ids);
    }
    
    /**
     * 审核通过
     */
    public function appr($ids = "")
    {
        if ($ids) {
            $pk = $this->model->getPk();
            $adminIds = $this->getDataLimitAdminIds();
            if (is_array($adminIds)) {
                $this->model->where($this->dataLimitField, 'in', $adminIds);
            }
            $list = $this->model->where($pk, 'in', $ids)->select();

            $count = 1;
            Db::startTrans();
            try {
                foreach ($list as $k => $v) {
                    $updatedata['updatetime'] =time();
                //    $appr_status = $v['certify_status']==2?'4':'5';
                    $updatedata['appr_status'] = 5;
                    $updatedata['appr_fail_reson'] =' ';
                   $res=Db::table('fa_user')->where('id', $v['id'])->update($updatedata);
                    //$count += $v->delete();
                }
                Db::commit();
            } catch (PDOException $e) {
                Db::rollback();
                $this->error($e->getMessage());
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if ($count) {
                $this->success();
            } else {
                $this->error(__('No rows were deleted'));
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }

    /**
     * 拒绝审核
     */
    public function reject($ids = "")
    {
        if ($ids) {
            $pk = $this->model->getPk();
            $adminIds = $this->getDataLimitAdminIds();
            if (is_array($adminIds)) {
                $this->model->where($this->dataLimitField, 'in', $adminIds);
            }
            $list = $this->model->where($pk, 'in', $ids)->select();

            $count = 1;
            Db::startTrans();


            try {
                foreach ($list as $k => $v) {
                    $updatedata['updatetime'] =time();
                    $updatedata['appr_status']=7;
                    $updatedata['certify_status']=2;
                    $updatedata['appr_fail_reson'] ='提交的材料信息有误,请重新上传营业执照！';
                   $res=Db::table('fa_user')->where('id', $v['id'])->update($updatedata);
                    //$count += $v->delete();
                }
                Db::commit();
            } catch (PDOException $e) {
                Db::rollback();
                $this->error($e->getMessage());
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if ($count) {
                $this->success();
            } else {
                $this->error(__('No rows were deleted'));
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }
    /**
     * 拒绝审核
     */
    public function unappr($ids = "")
    {
        if ($ids) {
            $pk = $this->model->getPk();
            $adminIds = $this->getDataLimitAdminIds();
            if (is_array($adminIds)) {
                $this->model->where($this->dataLimitField, 'in', $adminIds);
            }
            $list = $this->model->where($pk, 'in', $ids)->select();

            $count = 1;
            Db::startTrans();


            try {
                foreach ($list as $k => $v) {
                    $updatedata['updatetime'] =time();
                    $updatedata['appr_fail_reson'] ='提交的材料信息有误,请重新上传营业执照！';
                    $appr_status = $v['certify_status']==2?2:3;
                   $res=Db::table('fa_user')->where('id', $v['id'])->update($updatedata);
                    //$count += $v->delete();
                }
                Db::commit();
            } catch (PDOException $e) {
                Db::rollback();
                $this->error($e->getMessage());
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if ($count) {
                $this->success();
            } else {
                $this->error(__('No rows were deleted'));
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }


}
